package com.allwees.bs.core.model.product.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.allwees.bs.core.model.util.PhotosStr;
import com.allwees.bs.core.model.util.PriceShouldAdjusted;
import com.allwees.bs.core.modelbase.entity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.math.BigDecimal;

@Data
@EqualsAndHashCode(callSuper=true)
@ToString(callSuper = true)
@NoArgsConstructor
@MappedSuperclass
public class SkuModel extends BaseEntity implements PhotosStr, PriceShouldAdjusted {
	private static final long serialVersionUID = 2303690327646710714L;
	/**
	 * SKU编号（标准规范格式）
	 */
	@Column(unique = true, length = 128)
    protected String no;

	/**
	 * SKU编号（自定义）
	 */
	@Column(length = 64)
    protected String customNo;

    //SPU编号
	@Column(length = 64)
    protected String productNo;

    //商品
	@Column(length = 64)
    protected String productUuid;

    //颜色,在目前的商品分类下是颜色
	@Column(length = 32)
    protected String color;

    //尺码
	@Column(length = 64)
    protected String size;

	/**
	 * 重量
	 */
	protected Float weight;

	/**
	 * 重量单位
	 */
	@Column(length = 8)
	protected String weightUnit;

    //图片地址，多个图片用','拼接
	@JsonIgnore
    @JSONField(serialize = false, deserialize = false)
    @Column(length = 8192)
    private String photosStr;

    //原价
    @Column(precision = 8, scale = 2)
    protected BigDecimal originalPrice;

    //零售价
    @Column(precision = 8, scale = 2)
    protected BigDecimal retailPrice;

	/**
	 * 零售价币种
	 */
	@Column(length = 4)
	protected String currency;

    //库存量
    protected Integer quantity;

	/**
	 * 快递价格
	 */
    @Column(precision = 6, scale = 2)
	protected BigDecimal shippingPrice;

	/**
	 * 快递价格币种
	 */
	@Column(length = 4)
	protected String shippingCurrency;

	@JsonIgnore
    @JSONField(serialize = false)
	public String getValidNo() {
		return StringUtils.isNotBlank(customNo) ? customNo : no;
	}

	/**
	 * 调价后的sku原件
	 */
	@Transient
	@TableField(exist=false)
	protected BigDecimal priceOrigin;

	/**
	 * 调价后的sku售价
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	protected BigDecimal priceRetail;

	/**
	 * 调价后的运费
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	protected BigDecimal priceShip;


	public Integer getQuantity() {
		return this.quantity == null || ( this.quantity  < 0 ) ? 0 : this.quantity ;
	}

}
